package network.udp.udp_one2one;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-26 14:18
 **/
public class DatagramSend {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            //码头
            datagramSocket = new DatagramSocket();
            //package 打包
            byte[] bytes = "这是一个寂寞的天".getBytes();
            InetAddress localhost = InetAddress.getByName("localhost");
            DatagramPacket packet = new DatagramPacket(bytes,
                    bytes.length, localhost, 10000);

            //
            datagramSocket.send(packet);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }
    }
}
